package com.shadt.cloud.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shadt.cloud.dao.BranchDao;
import com.shadt.cloud.dao.ChannelDao;
import com.shadt.cloud.dao.RecordDao;
import com.shadt.cloud.entity.CloudBranch;
import com.shadt.cloud.service.RecordService;
import com.shadt.cloud.vo.ChannelVo;
import com.shadt.cloud.vo.RecordVo;
import com.shadt.core.util.DateUtil;

/**
 * 记录信息业务逻辑层实现类
 * 
 * @author SongR
 *
 */
@Service
public class RecordServiceImpl implements RecordService {
	private static final ResourceBundle bundle = java.util.ResourceBundle.getBundle("config");
	@Autowired
	RecordDao dao;
	
	@Autowired
	BranchDao bdao;

	@Autowired
	ChannelDao cdao;
	
	
	

	/**
	 * 实现根据key获取记录
	 */
	@SuppressWarnings("rawtypes")
	public List<RecordVo> get(String appname,String key, String id, Long size) throws Exception{
		List l = cdao.getBySql("select * from community_channel where isenable=1 and channelkey='" + key + "'");
		ChannelVo channelVo = null;
		if (l.size() > 0) {
			Map m = (Map) l.get(0);
			channelVo = new ChannelVo();
			channelVo.setId(m.get("seq").toString());
			channelVo.setName(m.get("title").toString());
			channelVo.setKey(m.get("channelkey").toString());
		}
		List<RecordVo> rvoss = new ArrayList<RecordVo>();
		if (null != channelVo) {
			CloudBranch b  = bdao.get("from CloudBranch where status=0 and name='" + appname + "'");
			String sql = "SELECT clc.seq rseq,r.* FROM community_record as r , "
					+ "(select rc.* from community_recordcloudrelease as rc where "
					+ "rc.channelKey='"+key+"' and rc.seq>"+id+") as clc WHERE "
					+ "clc.recordseq=r.seq  ";
			if(b.getStartTime()!=null&&!b.getStartTime().equals("")){
				sql+=" and r.createtime>='"+b.getStartTime()+"'";
			}
			sql +=" order by clc.seq asc limit 0,"+size;
			List rvos = dao.getBySql(sql);
			Iterator rit = rvos.iterator();
			while (rit.hasNext()) {
				Map mm = (Map) rit.next();
				RecordVo reo = new RecordVo();
				String recordid = mm.get("seq").toString();
				String sqls = "select * from community_recorddata a,community_field b where a.fieldseq = b.seq and a.recordseq="+ recordid;
				List ll = dao.getBySql(sqls);
				Iterator lit = ll.iterator();
				while (lit.hasNext()) {
					Map mmm = (Map) lit.next();
					if (mmm.get("fieldKey").equals("Crosshead")) {
						reo.setTitle(mmm.get("content").toString());
					} else if (mmm.get("fieldKey").equals("AnimationImg")) {
						reo.setCoverimg(mmm.get("content").toString());
					} else if (mmm.get("fieldKey").equals("RecordOne")) {
						reo.setImgA(mmm.get("content").toString());
					} else if (mmm.get("fieldKey").equals("RecordTwo")) {
						reo.setImgB(mmm.get("content").toString());
					} else if (mmm.get("fieldKey").equals("RecordThree")) {
						reo.setImgC(mmm.get("content").toString());
					}else if (mmm.get("fieldKey").equals("orderby")) {
						if(!mmm.get("content").equals("")&&mmm.get("content")!=null){
							reo.setSort(Integer.parseInt(mmm.get("content").toString()));
						}else{
							reo.setSort(0);
						}
					}
					reo.setId(mm.get("rseq").toString());
					reo.setUuid(mm.get("uniqueID").toString());
					reo.setUrl(bundle.getString("record.url")+"?uniqueID="+ mm.get("uniqueID").toString()+"&xgyd="+key+"&createTime="+
							DateUtil.dataToStr(DateUtil.strToDate(mm.get("createtime").toString(),"yyyy-MM-dd"), "yyyy-MM-dd"));
				}
				rvoss.add(reo);
			}
		}
		return rvoss;
	}
	@SuppressWarnings("rawtypes")
	@Override
	public List<RecordVo> get(String key,String startTime,String endTime,Long size,String id)
			throws Exception {
		List l = cdao.getBySql("select * from community_channel where isenable=1 and channelkey='" + key + "'");
		ChannelVo channelVo = null;
		if (l.size() > 0) {
			Map m = (Map) l.get(0);
			channelVo = new ChannelVo();
			channelVo.setId(m.get("seq").toString());
			channelVo.setName(m.get("title").toString());
			channelVo.setKey(m.get("channelkey").toString());
		}
		List<RecordVo> rvoss = new ArrayList<RecordVo>();
		if (null != channelVo) {
			String sql = null;
			if(endTime!=null && "".equals(endTime)){
				sql = "select r.*,cl.seq rseq from community_record as r, (select * from community_recordcloudrelease where seq>"+id+" limit 0,"+size+")" +
						" as cl where cl.recordseq=r.seq  and r.channelseq = '" + channelVo.getId() + "' " +
						" and updatetime BETWEEN FROM_UNIXTIME("+Integer.valueOf(startTime)+") AND FROM_UNIXTIME("+Integer.valueOf(endTime)+") order by cl.seq asc";
			}else{
				sql = "select r.*,cl.seq rseq from community_record as r, (select * from community_recordcloudrelease where seq>"+id+" limit 0,"+size+") " +
						" as cl where cl.recordseq=r.seq  and r.channelseq = '" + channelVo.getId() + "' " +
								" and updatetime>FROM_UNIXTIME("+Integer.valueOf(startTime)+") order by cl.seq asc";
			}
			List rvos = dao.getBySql(sql);
			Iterator rit = rvos.iterator();
			while (rit.hasNext()) {
				Map mm = (Map) rit.next();
				RecordVo reo = new RecordVo();
				String recordid = mm.get("seq").toString();
				String sqls = "select * from community_recorddata a,community_field b where a.fieldseq = b.seq and a.recordseq="+ recordid;
				List ll = dao.getBySql(sqls);
				Iterator lit = ll.iterator();
				while (lit.hasNext()) {
					Map mmm = (Map) lit.next();
					if (mmm.get("fieldKey").equals("Crosshead")) {
						reo.setTitle(mmm.get("content").toString());
					} else if (mmm.get("fieldKey").equals("AnimationImg")) {
						reo.setCoverimg(mmm.get("content").toString());
					} else if (mmm.get("fieldKey").equals("RecordOne")) {
						reo.setImgA(mmm.get("content").toString());
					} else if (mmm.get("fieldKey").equals("RecordTwo")) {
						reo.setImgB(mmm.get("content").toString());
					} else if (mmm.get("fieldKey").equals("RecordThree")) {
						reo.setImgC(mmm.get("content").toString());
					}
					reo.setId(mm.get("rseq").toString());
					reo.setUuid(mm.get("uniqueID").toString());
					reo.setUrl(bundle.getString("record.url")+"?uniqueID="+ mm.get("uniqueID").toString()+"&xgyd="+key+"&createTime="+
							DateUtil.dataToStr(DateUtil.strToDate(mm.get("createtime").toString(),"yyyy-MM-dd"), "yyyy-MM-dd"));
				}
				rvoss.add(reo);
			}
		}
		return rvoss;
	}
	
	
}
